//
//  WDPersonalInfoModifyTipView.m
//  wd-serviceapp
//
//  Created by Bin Tang on 2024/8/17.
//

#import "WDPersonalInfoModifyTipView.h"
#import "UIWindow+WDPersonalInfoModifyTipView.h"
#import "TYZTopLeftLabel.h"


@interface WDPersonalInfoModifyTipView ()

@property (nonatomic, copy) NSString *title;
@property (nonatomic, copy) NSString *desc;
@property (nonatomic, copy) NSString *imageTipName;

@property (nonatomic, copy) void(^completionBlock)(void);
@property (nonatomic, copy) void(^failedBlock)(void);

@property (nonatomic, strong) UIImageView *errorImgView;

@property (nonatomic, strong) UILabel *titleLabel;

@property (nonatomic, strong) TYZTopLeftLabel *descLabel;

@property (nonatomic, strong) UIButton *btnClose;

@end

@implementation WDPersonalInfoModifyTipView


- (void)initWithSubView
{
    self.backgroundColor = kWhiteColor;
    self.layer.cornerRadius = 8;
    self.layer.masksToBounds = YES;
    self.clipsToBounds = YES;
    
    [self initWithErrorImgView];
    
    [self initWithTitleLabel];

    [self initWithDescLabel];
    
    [self initWithBtnClose];
    
}

- (void)initWithErrorImgView {
    if (!_errorImgView) {
        CGRect frame = CGRectMake((self.width - 36) * 0.5, 24, 36, 36);
        _errorImgView = [UIImageView createWithImageView:self frame:frame defaultImage:[UIImage imageNamed:@"ic-error-tip"] propertiesBlock:^(UIImageView * _Nonnull imageView) {
            imageView.layer.cornerRadius = frame.size.height * 0.5;
            imageView.layer.masksToBounds = YES;
        }];
    }
    if (![objectNull(self.imageTipName) isEqualToString:@""]) {
        _errorImgView.image = [UIImage imageNamed:self.imageTipName];
    }
}

- (void)initWithTitleLabel {
    if (!_titleLabel) {
        CGRect frame = CGRectMake(20, _errorImgView.bottom + 10, self.width - 40, 25);
        _titleLabel = [UILabel createWithLabel:self frame:frame font:[UIFont systemFontOfSize:18 weight:UIFontWeightMedium] propertiesBlock:^(UILabel * _Nonnull label) {
            label.textColor = [UIColor colorWithHexString:@"#000000"];
            label.textAlignment = NSTextAlignmentCenter;
        }];
    }
    
    self.titleLabel.text = objectNull(self.title);
}

- (void)initWithDescLabel {
    if (!_descLabel) {
        CGRect frame = CGRectMake(24, self.titleLabel.bottom + 10, self.width - 48, 40);
        _descLabel = [TYZTopLeftLabel createWithLeftTopLabel:self frame:frame font:[UIFont systemFontOfSize:13 weight:UIFontWeightRegular] propertiesBlock:^(TYZTopLeftLabel * _Nonnull label) {
            label.textColor = [UIColor colorWithHexString:@"#000000"];
            label.text = @"显示返回的错误信息";
            label.textAlignment = NSTextAlignmentCenter;
            label.numberOfLines = 2;
        }];
//        _descLabel = [UILabel createWithLabel:self frame:frame font:[UIFont systemFontOfSize:13 weight:UIFontWeightRegular] propertiesBlock:^(UILabel * _Nonnull label) {
//            label.textColor = [UIColor colorWithHexString:@"#000000"];
//            label.text = @"显示返回的错误信息";
//            label.textAlignment = NSTextAlignmentCenter;
//            label.numberOfLines = 2;
//        }];
    }
    if (![objectNull(self.desc) isEqualToString:@""]) {
        self.descLabel.text = self.desc;
    }
}

- (void)initWithBtnClose {
    if (!_btnClose) {
        UIImage *image = [UIImage imageNamed:@"ic-tip-closed"];
        CGRect frame = CGRectMake(self.width - 30, 0, 30, 30);
        _btnClose = [UIButton createWithButton:self frame:frame font:FONTSIZE(15) target:self action:@selector(clickedCancel:) propertiesBlock:^(UIButton * _Nonnull button) {
            [button setImage:image forState:UIControlStateNormal];
        }];
    }
    if (![objectNull(self.imageTipName) isEqualToString:@""]) {
        self.errorImgView.image = [UIImage imageNamed:self.imageTipName];
    }
}


- (void)showWithTitle:(NSString *)title data:(id)data completion:(void (^)(void))completionBlock failed:(void (^)(void))failedBlock
{
    self.title = title;
    if (data) {
        self.desc = data[@"desc"];
        self.imageTipName = data[@"imageTipName"];
    }
    self.completionBlock = completionBlock;
    self.failedBlock = failedBlock;
    if (!kCSKeyWindow.personalInfoModifyTipView)
    {
        // 创建蒙版
        UIView *maskView = [[UIView alloc] initWithFrame:[UIScreen mainScreen].bounds];
        maskView.backgroundColor = [UIColor blackColor];
        maskView.alpha = 0;
        [kCSKeyWindow addSubview:maskView];
        kCSKeyWindow.personalInfoModifyTipMaskView = maskView;
        
        [kCSKeyWindow addSubview:self];
        kCSKeyWindow.personalInfoModifyTipView = self;
        
//        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(clickedSubmit:)];
//        [maskView addGestureRecognizer:tap];
//        [self addGestureRecognizer:tap];
        
        [UIView animateWithDuration:0.2 animations:^{
            maskView.alpha = 0.4;
            self.alpha = 1;
        }];
    }
    [self initWithTitleLabel];
    
    [self initWithDescLabel];
    
    [self initWithErrorImgView];
}

- (void)clickedCancel:(id)sender
{
    if (self.failedBlock)
    {
        self.failedBlock();
    }
}

- (void)clickedSubmit:(id)sender
{
    if (self.completionBlock)
    {
        self.completionBlock();
    }
}

- (void)hide
{
    if (kCSKeyWindow.personalInfoModifyTipView)
    {
        @weakify(self);
        [UIView animateWithDuration:0.2 animations:^{
            @strongify(self);
            self.alpha = 0;
            kCSKeyWindow.personalInfoModifyTipMaskView.alpha = 0;
        } completion:^(BOOL finished) {
//            @strongify(self);
//            if (self.completionBlock)
//            {
//                self.completionBlock(nil);
//            }
            [kCSKeyWindow.personalInfoModifyTipView removeFromSuperview];
            [kCSKeyWindow.personalInfoModifyTipMaskView removeFromSuperview];
            kCSKeyWindow.personalInfoModifyTipMaskView = nil;
            kCSKeyWindow.personalInfoModifyTipView = nil;
        }];
    }
}


@end
